﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NotificationServer.BusinessLayer;
using NotificationServer.ServiceLayer.Contracts;
using NotificationServer.ServiceLayer.Data;

namespace NotificationServer.ServiceLayer.Services
{
    public partial class Service : IDeviceForMobile 
    {
        private DeviceBO _deviceBO;

        public DeviceData RegisterDevice(string name, string device_type, string registration_id)
        {
            Console.WriteLine(string.Empty);
            DeviceData deviceData = new DeviceData();
            string insertedGuid = String.Empty;
            try
            {                
                _deviceBO = new DeviceBO();
                insertedGuid = _deviceBO.RegisterDevice(name, device_type, registration_id);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("RegisterDevice: ");

                if (insertedGuid == null)
                {
                    Console.Write("Unabel to register device");
                    deviceData.Error = true;
                    deviceData.ReturnMessage = "Unabele to register device";
                    deviceData.Guid = String.Empty;
                    return deviceData;
                }  
            }catch(Exception e)
            {
                deviceData.Error = true;
                deviceData.ReturnMessage = e.Message;
                deviceData.Guid = String.Empty;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(e.Message);
            }

            Console.Write("Device registered successfully: " + insertedGuid);
            deviceData.Error = false;
            deviceData.Guid = insertedGuid;
            deviceData.ReturnMessage = "Device registered successfully";
            return deviceData;
        }


        public DeviceData UpdateDevice(string guid, string name, string registration_id)
        {
            Console.WriteLine(string.Empty);
            Console.ForegroundColor = ConsoleColor.Green;
            DeviceData deviceData = new DeviceData();
            int affectedRows = 0;
            try
            {
                _deviceBO = new DeviceBO();
                affectedRows = _deviceBO.UpdateDevice(guid, name, registration_id);
                
            }            
            catch(Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                deviceData.Guid = guid;
                deviceData.Error = true;
                deviceData.ReturnMessage = String.Format(ex.Message);                
                Console.WriteLine(ex.Message);
                return deviceData;
            }

            if (affectedRows > 0)
            {
                deviceData.Guid = guid;
                deviceData.Error = false;
                deviceData.ReturnMessage = "Your device is updated successfully";
                Console.WriteLine("Your device is updated successfully");
            }
            else
            {
                deviceData.Guid = guid;
                deviceData.Error = true;
                deviceData.ReturnMessage = String.Format("The GUID: {0} couldn\'t be found in database", guid);
                Console.WriteLine("The GUID: {0} couldn\'t be found in database", guid);
            }
            return deviceData;
        }

        public DeviceData UnregisterDevice(string guid)
        {
            Console.WriteLine(string.Empty);
            Console.ForegroundColor = ConsoleColor.Green;
            DeviceData deviceData = new DeviceData();
            int affectedRows = 0;
            try
            {
                _deviceBO = new DeviceBO();
                affectedRows = _deviceBO.UnregisterDevice(guid);
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                deviceData.Guid = guid;
                deviceData.Error = true;
                deviceData.ReturnMessage = String.Format(ex.Message);
                Console.WriteLine(ex.Message);
                return deviceData;
            }

            if (affectedRows > 0)
            {
                deviceData.Guid = guid;
                deviceData.Error = false;
                deviceData.ReturnMessage = "Your device is unregistred successfully";
                Console.WriteLine("Your device is unregistred successfully");
            }
            else
            {
                deviceData.Guid = guid;
                deviceData.Error = true;
                deviceData.ReturnMessage = String.Format("The GUID: {0} couldn\'t be found in database", guid);
                Console.WriteLine("The GUID: {0} couldn\'t be found in database", guid);
            }
            return deviceData;
        }

        public Device[] GetDevices()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("GetAllDevices");

            _deviceBO = new DeviceBO();
            return _deviceBO.GetDevices();
        }
    }
}